package com.gary.shiro.service;

import com.gary.shiro.dao.RoleDAO;
import com.gary.shiro.po.Role;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author peng_rui
 * @date 2022/3/26
 */
@Slf4j
@Service
public class RoleService {
    @Resource
    private RoleDAO roleDAO;

    /**
     * 根据角色ids查询
     * @param roleIds
     * @return
     */
    public Set<String> queryRoleByIds(List<Long> roleIds) {
        Set<String> roles = new HashSet<>();
        if (CollectionUtils.isEmpty(roleIds)) {
            return roles;
        }
        Example example = new Example(Role.class);
        example.createCriteria().andIn("id", roleIds);
        List<Role> roleList = roleDAO.selectByExample(example);
        if (CollectionUtils.isEmpty(roleList)) {
            return roles;
        }
        roles = roleList.stream().map(Role::getName).collect(Collectors.toSet());
        return roles;
    }
}
